import * as Koa from 'koa';
import { redis_port, redis_host } from './config/default.config';
import * as redis from 'redis';
import * as redisStore from 'koa-redis';
import * as session from 'koa-generic-session';
import * as bodyParser from 'koa-bodyparser';
import * as koaStatic from 'koa-static';
import * as logger from 'koa-logger';
import { registerRouter } from './router';
import * as json from 'koa-json';
// 数据库
// import 'reflect-metadata';
// import { createConnection } from 'typeorm';
import { Connection } from './config/database';

const app = new Koa();

Connection()
  .then((conn: any) => {
    console.log('数据库已连接');
    app.use(koaStatic('./webui'));
    // redis
    let client = redis.createClient(redis_port, redis_host);
    app.use(
      session({
        store: redisStore({
          client: client
        })
      })
    );
    app.use(logger());
    app.use(bodyParser()); // 在路由前面写，post请求参数接收才会生效
    app.use(registerRouter());
    app.use(json());
    const httpPort = 8080;

    app.listen(httpPort);

    console.log(`Http Server running on port ${httpPort}`);
  })
  .catch((err: any) => console.log('数据库连接失败！', err));
